<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Form;

use Illuminate\Http\UploadedFile;
use Encore\Admin\Grid;
use App\Models\GameServer;
use Illuminate\Http\Request;

use \Illuminate\Support\Facades\Input;
class HomeController extends Controller
{
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Dashboard');
            $content->description('server current time'. date("Y-m-d H:i:s", time()));

            $content->row(Dashboard::title());

            $content->row(function (Row $row) {

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::environment());
                });

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::extensions());
                });

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::dependencies());
                });
            });
        });
    }

    public function test(){
	return Admin::content(function (Content $content) {

            $content->header('chart');
            $content->description('.....');

            //$content->body(view('admin.charts.test'));
            $content->body(view('admin.charts.test_echart'));
		
            //$content->body(phpinfo());
        });
    }

    /**
     * code = 0 成功 else 不成功
     * msg 提示信息
     * redirect 跳转url
     * auto 5秒自动跳转
     */
    public function redirect(){
        $get = Input::all();
        return Admin::content(function($content)use($get){
            
            $tpl = 'admin.success';
            $data['code'] = $get['code'];

            if (!$get['code']) {
                $data['msg'] = $get['msg'] ?: 'success';
            } else {
                //$tpl = 'admin.error';
                $data['msg'] = $get['msg'] ?: 'error';
            }
            
            $data['redirect'] = $get['redirect'];//跳转url
            $data['auto'] = $get['auto']?:0;//自动跳转
            
            $content->body(view($tpl, $data));
        });
    }

    public function testUploadFile() {
        return Admin::content(function($content) use($data){
		    $content->header('测试上传');
            $grid = Admin::Form(\App\Models\EMail::class, function(Form $form)use($data){
                
                //$one->area_list = explode(',', $one->area_list);

                $form->text('title', 'title');
                $form->textarea('content', 'content');
                $form->file('test_upload', 'Test Upload');
                $form->image('test_image', 'Test Image');


                // $form->saved(function (Form $form) {
                //     \Log::debug(json_encode(['data'=>'save click']));
                //     admin_toastr('laravel-admin 提示','success');
                //     return response('success');
                // });

                $form->disablePjax();
                $form->setAction('/admin/home/test-upload');

            });
            $content->row($grid);
        });
    }

    public function testUpload(Request $request) {
        \Log::debug('test upload file $_FILES:', [$_FILES, Input::all()]);
        \Log::debug('test upload file laravel request:', [$request->file('test_upload'), $request->file('test_image')]);
    }

}
